<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简易手机通讯录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 80px;
            font-weight: bold;
        }
        input[type="text"] {
            padding: 8px;
            width: 200px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .button-group {
            margin: 20px 0;
            text-align: center;
        }
        button {
            padding: 8px 15px;
            margin: 0 5px;
            border: none;
            border-radius: 10px;
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
            font-size: 14px;
        }
        button:hover {
            background-color: #45a049;
        }
        #resetBtn {
            background-color: #f44336;
        }
        #resetBtn:hover {
            background-color: #d32f2f;
        }
        #deleteBtn {
            background-color: #ff9800;
        }
        #deleteBtn:hover {
            background-color: #e68a00;
        }
        textarea {
            width: 100%;
            height: 200px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
        }
        .message {
            margin: 10px 0;
            padding: 10px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
            display: block;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
            display: block;
        }
    </style>
</head>
<body>
    <h1>简易手机通讯录</h1>

    <div class="form-group">
        <label for="name">姓名：</label>
        <input type="text" id="name" placeholder="请输入姓名">
    </div>

    <div class="form-group">
        <label for="phone">电话：</label>
        <input type="text" id="phone" placeholder="请输入电话">
    </div>

    <div class="button-group">
        <button id="addBtn">添加</button>
        <button id="searchBtn">查询</button>
        <button id="deleteBtn">删除</button>
        <button id="resetBtn">重置</button>
    </div>

    <div id="message" class="message"></div>

    <textarea id="contactList" readonly placeholder="通讯录将显示在这里..."></textarea>

    <script>
        // 获取DOM元素
        const nameInput = document.getElementById('name');
        const phoneInput = document.getElementById('phone');
        const addBtn = document.getElementById('addBtn');
        const searchBtn = document.getElementById('searchBtn');
        const deleteBtn = document.getElementById('deleteBtn');
        const resetBtn = document.getElementById('resetBtn');
        const contactList = document.getElementById('contactList');
        const message = document.getElementById('message');

        // 初始化通讯录
        if (!localStorage.getItem('phoneBook')) {
            localStorage.setItem('phoneBook', JSON.stringify({}));
        }

        // 更新通讯录显示
        function updateContactList() {
            const phoneBook = JSON.parse(localStorage.getItem('phoneBook'));
            let listContent = '';

            for (const name in phoneBook) {
                listContent += `${name}: ${phoneBook[name]}\n`;
            }

            contactList.value = listContent || "通讯录为空";
        }

        // 显示消息
        function showMessage(msg, isSuccess) {
            message.textContent = msg;
            message.className = isSuccess ? 'message success' : 'message error';
            setTimeout(() => {
                message.style.display = 'none';
            }, 3000);
        }

        // 添加联系人
        addBtn.addEventListener('click', function() {
            const name = nameInput.value.trim();
            const phone = phoneInput.value.trim();

            if (!name || !phone) {
                showMessage('姓名和电话不能为空', false);
                return;
            }

            const phoneBook = JSON.parse(localStorage.getItem('phoneBook'));
            phoneBook[name] = phone;
            localStorage.setItem('phoneBook', JSON.stringify(phoneBook));

            showMessage('联系人添加成功', true);
            updateContactList();
        });

        // 查询联系人
        searchBtn.addEventListener('click', function() {
            const name = nameInput.value.trim();

            if (!name) {
                showMessage('请输入要查询的姓名', false);
                return;
            }

            const phoneBook = JSON.parse(localStorage.getItem('phoneBook'));

            if (phoneBook.hasOwnProperty(name)) {
                phoneInput.value = phoneBook[name];
                showMessage('查询成功', true);
            } else {
                phoneInput.value = '';
                showMessage('未找到该联系人', false);
            }
        });

        // 删除联系人
        deleteBtn.addEventListener('click', function() {
            const name = nameInput.value.trim();

            if (!name) {
                showMessage('请输入要删除的姓名', false);
                return;
            }

            const phoneBook = JSON.parse(localStorage.getItem('phoneBook'));

            if (phoneBook.hasOwnProperty(name)) {
                delete phoneBook[name];
                localStorage.setItem('phoneBook', JSON.stringify(phoneBook));

                nameInput.value = '';
                phoneInput.value = '';
                showMessage('联系人删除成功', true);
                updateContactList();
            } else {
                showMessage('未找到该联系人', false);
            }
        });

        // 重置表单
        resetBtn.addEventListener('click', function() {
            nameInput.value = '';
            phoneInput.value = '';
            message.style.display = 'none';
        });

        // 初始化显示
        updateContactList();
    </script>
</body>
</html>